<?php 

class TabsHelper extends AppHelper 
{
	var $tabs = array();
	var $helpers = array('Javascript', 'Html');
	static $count = 1;
	
	function beforeRender()
	{
		$this->addJUI();	
	}
	
	function addTab($heading, $content, $properties = array())
	{
		$defaultProps = array(
			'tab_id' => 'tab_'.rand(5,2000).'_'.++self::$count
		);
		
		$properties = am($defaultProps, $properties);
		$this->tabs[] = compact('heading', 'content', 'properties');
	}
	
	function show($properties = array())
	{
		$defaultProps = array(
			'container_id' => 'tab_container',
			'output_js' => false  // needed sometimes for hacks
		);
		
		$properties = am($defaultProps, $properties);
		$content = null;
		
		$content .= '<div id="'.$properties['container_id'].'">';
		
		$content .= '<ul class="nav nav-tabs">';
		foreach($this->tabs as $key=>$tab)
		{
                    $cls = '';
                    if($key == 0)
                        $cls = ' class="active"';
			$content .= '<li'.$cls.'><a href="#'.$tab['properties']['tab_id'].'" data-toggle="tab">'.$tab['heading'].'</a><i class="fa fa-caret-down"></i></li>';
		}
		$content .= '</ul>';
                $content .= '<div class="tab-content">';
		foreach($this->tabs as $key=>$tab)
		{
                    $cls = '';
                    if($key == 0)
                        $cls = 'in active';
			$content .= '<div class="tab-pane fade '.$cls.'" id="'.$tab['properties']['tab_id'].'">'.$tab['content'].'</div>';
		}
		$content .= '</div>';
				
		$js = "$('#".$properties['container_id']."').tabs();";
		if($properties['output_js'])
		{
			$content .= $this->Javascript->wrapDocumentReady($js);
		}
		else
		{
			$this->Javascript->startup($js);
		}
		$this->tabs = array();
		return $content;	
	}
	
	
}

?>